﻿using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace TaoNiuOA.Models
{
    /// <summary>
    /// 用户基本信息
    /// </summary>
    public class User
    {
        [Key]
        public int UserID { get; set; }

        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Display(Name = "密码")]
        [DataType(DataType.Password)]
        [StringLength(32, ErrorMessage = "密码必须至少{2}并且不能大于{1}个字符。", MinimumLength = 6)]
        public string Password { get; set; }

        [Display(Name = "邮箱")]
        [DataType(DataType.EmailAddress, ErrorMessage = "请输入正确的邮箱地址.")]
        public string Email { get; set; }

        [Display(Name = "密码保护问题")]
        public string PasswordQuestion { get; set; }

        [Display(Name = "密码保护答案")]
        public string PasswordAnswer { get; set; }

        [Display(Name = "注册时间")]
        public DateTime? RegTime { get; set; }

        [Display(Name = "最后活动时间")]
        public DateTime? LastActivityTime { get; set; }

        [Display(Name = "用户详细信息")]
        public int UserInfoID { get; set; }

        [Display(Name = "部门")]
        public int DepartmentID { get; set; }

        [Display(Name = "职位")]
        public int UserGroupID { get; set; }

        public int UserRoleID { get; set; }

        //帐号状态,0为已禁用,1为正常
        [Display(Name = "帐号状态")]
        public int Status { get; set; }

        DbEntity db = new DbEntity();
        /// <summary>
        /// 该用户的详细信息
        /// </summary>
        /// <returns></returns>
        public UserInfo GetUserInfo()
        {
            var ui = db.UserInfoes.SingleOrDefault(n => n.UserInfoID == this.UserInfoID);
            if (ui == null)
            {
                ui = new UserInfo();
            }
            return ui;
        }

        /// <summary>
        /// 获取用户的职位
        /// </summary>
        /// <returns></returns>
        public UserGroup GetUserGroup()
        {
            var ug = db.UserGroups.SingleOrDefault(n => n.UserGroupID == this.UserGroupID);
            if (ug == null)
            {
                ug = new UserGroup();
            }
            return ug;
        }

        /// <summary>
        /// 获取用户的部门
        /// </summary>
        /// <returns></returns>
        public Department GetDepartment()
        {
            var depart = db.Departments.SingleOrDefault(n => n.DepartmentID == this.DepartmentID);
            if (depart == null)
            {
                depart = new Department();
            }
            return depart;
        }

        /// <summary>
        /// 显示当前账号状态
        /// </summary>
        /// <returns></returns>
        public string ShowStatus()
        {
            if (this.Status == 0)
                return "禁用";
            else
                return "正常";
        }
    }

    public class ChangePasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "当前密码")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(32, ErrorMessage = "密码必须至少{2}并且不能大于{1}个字符。", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "新密码")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "确认新密码")]
        [Compare("NewPassword", ErrorMessage = "新密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }

    public class LogOnModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        [StringLength(32, ErrorMessage = "密码必须至少{2}并且不能大于{1}个字符。", MinimumLength = 6)]
        public string Password { get; set; }

        [Display(Name = "记住我?")]
        public bool RememberMe { get; set; }
    }

    public class AddUserModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [StringLength(32, ErrorMessage = "密码必须至少{2}并且不能大于{1}个字符。", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "确认密码")]
        [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }

    }

    public class RegisterModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [StringLength(32, ErrorMessage = "密码必须至少{2}并且不能大于{1}个字符。", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "确认密码")]
        [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }

        [Required]
        [Display(Name = "邮箱")]
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "请输入正确的邮箱地址.")]
        public string Email { get; set; }

        [Required]
        [Display(Name = "密码保护问题")]
        public string PasswordQuestion { get; set; }

        [Required]
        [Display(Name = "密码保护答案")]
        public string PasswordAnswer { get; set; }
    }
}
